﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Shared;
using ElectronicShoppingMall.Service.Services.Client;
using ElectronicShoppingMall.Web.Front.DTOS;
using Extension;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Http;

namespace ElectronicShoppingMall.Web.Front.Controllers
{
    public class GoodsController : ApiController
    {
        private readonly GoodsService _goodsService;
        public GoodsController()
        {
            _goodsService = new GoodsService();
        }

        [HttpPost]
        public List<GoodsInfoDTO> GetGoodsInfo([FromBody]string category)
        {
            List<GoodsInfoDTO> result = new List<GoodsInfoDTO>();
            var goodsResults = _goodsService.GetAllGoodsByCategory(category);
            foreach (var goods in goodsResults)
            {
                GoodsInfoDTO temp = new GoodsInfoDTO
                {
                    Category = goods.Category,
                    Describe = goods.Describe,
                    Details = goods.Details,
                    GoodsId = goods.GoodsId,
                    GoodsName = goods.GoodsName,
                    Price = goods.Price.ToString(CultureInfo.InvariantCulture),
                    Stock = goods.Stock.ToString(),
                    Images = new List<string>()
                };
                foreach (var img in goods.Images)
                {
                    temp.Images.Add(img.ImagePath);
                }
                result.Add(temp);
            }
            return result;
        }

        [HttpGet]
        public GoodsInfoDTO GetGoodsInfoByGoodsId(string goodsId)
        {
            try
            {
                var goods = _goodsService.GetGoodsInfoByGoodsId(Convert.ToInt32(goodsId));
                var result = new GoodsInfoDTO
                {
                    Category = goods.Category,
                    Describe = goods.Describe,
                    Details = goods.Details,
                    GoodsId = goods.GoodsId,
                    GoodsName = goods.GoodsName,
                    Price = goods.Price.ToString(CultureInfo.InvariantCulture),
                    Stock = goods.Stock.ToString(),
                    Images = new List<string>()
                };
                foreach (var img in goods.Images)
                {
                    result.Images.Add(img.ImagePath);
                }
                return result;
            }
            catch
            {
                return null;
            }

        }

        [HttpGet]
        [Authorize]
        public List<CartGoodsInfoDTO> GetGoodsInfoByUserId(string userId)
        {
            try
            {
                List<CartGoodsInfoDTO> result = new List<CartGoodsInfoDTO>();
                var goodsInfo = _goodsService.GetCartGoodsInfoByUserId(Convert.ToInt32(userId));
                foreach (var goods in goodsInfo)
                {
                    CartGoodsInfoDTO temp = new CartGoodsInfoDTO
                    {
                        Describe = goods.Key.Describe,
                        Details = goods.Key.Details,
                        GoodsId = goods.Key.GoodsId,
                        GoodsName = goods.Key.GoodsName,
                        Price = goods.Key.Price.ToString(CultureInfo.InvariantCulture),
                        Number = goods.Value
                    };
                    foreach (var img in goods.Key.Images)
                    {
                        temp.Image = img.ImagePath;
                        break;
                    }
                    result.Add(temp);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        [HttpGet]
        [Authorize]
        public DeliveryInfoDTO GetClientDeliveryInfo()
        {
            string token = Request.Headers.Authorization.ToString();
            var userId = ExtensionMethods.GetClientUserObject(token).UserId;
            try
            {
                var deliveryAddress = _goodsService.GetClientDeliveryInfoByUserId(userId);
                var result = new DeliveryInfoDTO
                {
                    Consignee = deliveryAddress.Consignee,
                    DetailedAddress = deliveryAddress.DetailedAddress,
                    PhoneNumber = deliveryAddress.PhoneNumber
                };
                return result;
            }
            catch
            {
                return null;
            }
        }

        [HttpPut]
        [Authorize]
        public String AddGoodsToCart(dynamic obj)
        {
            try
            {

                return _goodsService.AddGoodsToCart((int)obj.userId, (int)obj.goodsId, (int)obj.number);
            }
            catch
            {
                return null;
            }
        }

        [HttpDelete]
        [Authorize]
        public List<CartGoodsInfoDTO> DeleteGoodsFromCart(dynamic obj)
        {
            try
            {
                _goodsService.DeleteGoodsFromCart((int)obj.userId, (int)obj.goodsId);
                return GetGoodsInfoByUserId((string)obj.userId);
            }
            catch
            {
                return null;
            }

        }
        [HttpPatch]
        public List<NewProductModel> GetNewProduct()
        {
            try
            {
                return _goodsService.GetNewProduct();
            }
            catch
            {
                return null;
            }
        }

    }
}
